package com.zhongwei.lvzhixuanmanage.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongwei.lvzhixuanmanage.entity.Order;
import com.zhongwei.lvzhixuanmanage.vo.OrderDetail;
import com.zhongwei.lvzhixuanmanage.vo.OrderGoods;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 施灿彬
 * @Date: 2022/07/05
 * @Description:
 */
@Repository
public interface OrderMapper extends BaseMapper<Order> {
    String wrapperSql = "SELECT o.id,o.order_no,o.pay_status,o.shop_name,o.buyer_user_name,o.pay_amount,o.status,o.order_type,o.order_pay_type,o.addr_type,o.source_flag,o.pay_time,op.pay_no\n" +
            "FROM t_order o LEFT JOIN t_order_pay op on o.order_no = op.order_no ${ew.customSqlSegment}";

    @Select(wrapperSql)
    IPage<Order> page(Page page, @Param("ew") Wrapper queryWrapper);

    @Select("SELECT o.id,o.order_no,o.pay_status,o.shop_name,o.buyer_user_name,o.pay_amount,o.status,o.order_type,o.order_pay_type,o.addr_type,o.source_flag,o.pay_time,op.pay_no FROM t_order o LEFT JOIN t_order_pay op on o.order_no = op.order_no")
    List<Order> selectOrders();

    @Select("SELECT o.order_no,o.pay_time,o.pay_amount,o.source_flag,o.buyer_user_name,\n" +
            "o.`status`,o.buy_remark,o.suppler_user_id,o.suppler_user_name,o.shop_name,o.sup_remark,o.order_type,\n" +
            "o.addr_type,o.buyer_user_id\n" +
            "FROM t_order o\n" +
            "where o.order_no = #{orderNo}")
    @Results(id = "order_no", value = {
            @Result(column = "pay_time", property = "payTime"),
            @Result(column = "order_no", property = "orderNo"),
            @Result(column = "pay_amount", property = "payAmount"),
            @Result(column = "source_flag", property = "sourceFlag"),
            @Result(column = "buyer_user_name", property = "buyerUserName"),
            @Result(column = "status", property = "status"),
            @Result(column = "suppler_user_id", property = "supplerUserId"),
            @Result(column = "suppler_user_name", property = "supplerUserName"),
            @Result(column = "shop_name", property = "shopName"),
            @Result(column = "sup_remark", property = "supRemark"),
            @Result(column = "sup_remark", property = "supRemark"),
            @Result(column = "order_type", property = "orderType"),
            @Result(column = "addr_type", property = "addrType"),
            @Result(column = "buyer_user_id", property = "companyName", javaType = String.class,
                    one = @One(select = "com.zhongwei.lvzhixuanmanage.mapper.OrderMapper.selectCompanyNameByUserId")
            ),
            @Result(column = "order_no", property = "orderGoods", javaType = List.class,
                    many = @Many(select = "com.zhongwei.lvzhixuanmanage.mapper.OrderMapper.selectGoodsByOrderNo"))
    })
    OrderDetail selectByOrderNo(@Param("orderNo")String orderNo);

    @Select("select og.id,og.goods_img,og.goods_name,og.goods_count,og.goods_fee,og.goods_price,\n" +
            "ogs.spec_values,ogs.is_refund,ogs.goods_sett_price,ogs.goods_modify_price\n" +
            "from t_order_goods og\n" +
            "INNER JOIN t_order_goods_spec ogs ON ogs.order_no = og.order_no and  ogs.goods_id = og.goods_id\n" +
            "where og.order_no = #{orderNo}")
    @Results(id = "id",value = {
            @Result(property = "goodsImg", column = "goods_img"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "goodsName", column = "goods_name"),
            @Result(property = "goodsCount", column = "goods_count"),
            @Result(property = "goodsName", column = "goods_name"),
            @Result(property = "goodsFee", column = "goods_fee"),
            @Result(property = "goodsPrice", column = "goods_price"),
            @Result(property = "isRefund", column = "is_refund"),
            @Result(property = "goodsSettPrice", column = "goods_sett_price"),
            @Result(property = "goodsModifyPrice", column = "goods_modify_price")
    })
    List<OrderGoods> selectGoodsByOrderNo(@Param("orderNo") String orderNo);

    @Select("SELECT DISTINCT ue.company_name FROM t_order o INNER JOIN t_user_enterprise ue ON o.buyer_user_id = ue.user_id WHERE ue.user_id = #{buyerUserId};")
    String selectCompanyNameByUserId(@Param("buyerUserId") String buyerUserId);
}
